<?php
/**
 * Eren Framework
 * 
 * The adapter of Cache operation
 * 
 * @category System
 * @author Manu Soros Leu
 * @copyright manusoros@gmail.com
 */

class Eren_Cache_Adapter
{
    /**
     * The instance of Session class like 'Eren_Cache_File'
     *
     * @var Eren_Session_Abstract
     */
    protected $_cache = NULL;
    
    /**
     * Init class
     * 
     * @param  Eren_Cache_Abstract  $cache  Instance of Cache class which inherit from Eren_Cache_Abstract
     * @throws Eren_Cache_Exception
     */
    public function __construct($cache)
    {
        if(!Eren_Validator::isBaseClass($cache, 'Eren_Cache_Abstract')){
            throw new Eren_Cache_Exception('The param of construct of Eren_Cache_Adapter must inherit from Eren_Cache_Abstract');
        }
        $this->_cache = $cache;
    }

    /**
     * Get value from Cache
     * 
     * @param string $key
     * @param string|int $default
     * @throws Eren_Cache_Exception
     * @return string|int..
     */
    public function get($key, $default = NULL)
    {
        if(is_array($key) || $key == NULL){
            throw new Eren_Cache_Exception('The param of Eren_Cache_Adapter::get must be string and cant be NULL');
        }
        return $this->_cache->get($key, $default);
    }
    
    /**
     * Set value into Cache
     * 
     * @param string $key
     * @param string|int $value
     * @param int $expireTime  seconds
     * @throws Eren_Cache_Exception
     * @return bool
     */
    public function set($key, $value, $expireTime = 3600)
    {
        if(is_array($key) || $key == NULL){
            throw new Eren_Cache_Exception('The first param of Eren_Cache_Adapter::set must be string and cant be NULL');
        }
        return $this->_cache->set($key, $value, $expireTime);
    }
    
    /**
     * Clean cache by Key
     * 
     * @param string $key
     * @throws Eren_Cache_Exception
     * @return bool
     */
    public function clean($key)
    {
        if(is_array($key) || $key == NULL){
            throw new Eren_Cache_Exception('The param of Eren_Cache_Adapter::clean must be string and cant be NULL');
        }
        return $this->_cache->clean($key);
    }
}